package com.authority.utils;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;


public class HttpClientPool {

	/** 设置连接的超时时间 */
	private static int connectionTimeout = 2000;
	/** 读取数据的超时时间 */
	private static int soTimeout = 3000;
	/** 设置每个路由最大连接数 */
	private static int defaultMaxPerRoute = 5;
	/** 设置最大连接数 */
	private static int maxTotal = 500;

	public static PoolingHttpClientConnectionManager connectionManager;

	static {
		// SchemeRegistry schemeRegistry = new SchemeRegistry();
		// schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory
		// .getSocketFactory()));
		//
		 connectionManager = new PoolingHttpClientConnectionManager();
		 connectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
		 connectionManager.setMaxTotal(maxTotal);
		//
		// httpParams = new BasicHttpParams();
		// httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
		// connectionTimeout);
		// httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, soTimeout);

		RequestConfig config = RequestConfig.custom()
				.setConnectTimeout(connectionTimeout)
				.setSocketTimeout(soTimeout).build();
		
		httpClient = HttpClientBuilder.create()
				.setConnectionManager(connectionManager)
				.setDefaultRequestConfig(config).build();
		
	}

	public static CloseableHttpClient getHttpClient() {
		// return new DefaultHttpClient(connectionManager, httpParams);
		return httpClient;
	}

	private static final CloseableHttpClient httpClient;
	public static final String CHARSET = "UTF-8";

}
